<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
  		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>网站后台管理模版</title>
		<link rel="stylesheet" type="text/css" th:href="@{/layui/css/layui.css}"/>
		<link rel="stylesheet" type="text/css" th:href="@{/css/admin.css}"/>
		<link th:href="@{/lib/font-awesome/css/font-awesome.css}" rel="stylesheet" />
	</head>
	<body>
	<form class="layui-form column-content-detail" id="actAdd">
		<div class="layui-tab">
			<ul class="layui-tab-title">
				<li class="layui-this">添加图片</li>
			</ul>
			<div class="layui-tab-content">
				<div class="layui-tab-item layui-show">
					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">图片类型：</label>
							<div class="layui-input-block">
								<select name="picTypeValue" lay-filter="status" id="picTypeValue">
									<option value="" selected disabled>请选择一个图片类型</option>
									<option th:each="biao,userStat:${picTypes}" th:value="${biao.dictdataValue}" th:text="${biao.dictdataName}"></option>
								</select>
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">文件名称：</label>
							<div class="layui-input-block">
								<input type="text" id="name" name="name"  required lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">文件排序：</label>
							<div class="layui-input-block">
								<input type="number" min="0" id="picSort" name="picSort"  required lay-verify="required" placeholder="请输入文件排序" autocomplete="off" class="layui-input">
							</div>
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">跳转链接：</label>
						<div class="layui-input-block">
							<input type="text" id="turnUrl" name="turnUrl"  required lay-verify="required" placeholder="请输入跳转链接" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">上传文件：</label>
							<div class="layui-upload-drag" id="test10" style="height: 120px;">
								<i class="layui-icon"></i>
								<p>点击上传，或将文件拖拽到此处</p>
							</div>
						</div>
						<div class="layui-inline">
							<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;width: 500px;height: 150px; ">
								预览图：
								<div class="layui-upload-list" id="demo2"></div>
								<input type="hidden" id="src">
							</blockquote>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="layui-form-item" style="padding-left: 10px;">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" type="submit">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
	<script th:src="@{/js/jquery-3.2.1.min.js}" type="text/javascript" charset="utf-8"></script>
	<script th:src="@{/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
	<script th:src="@{/js/common.js}" type="text/javascript" charset="utf-8"></script>
	<script>

            layui.use(['form', 'table', 'laydate', 'layer', 'laypage', 'dialog',  'tool', 'element', 'upload', 'layedit','upload'], function() {
				var form = layui.form,
					layer = layui.layer,
                    table = layui.table,
					laypage = layui.laypage,
					laydate = layui.laydate,
					layedit = layui.layedit,
					tool = layui.tool,
					element = layui.element,
					dialog = layui.dialog,
                    upload = layui.upload;

				//获取当前iframe的name值
				var iframeObj = $(window.frameElement).attr('name');

				//拖拽上传
				upload.render({
					elem: '#test10'
					,url: '/uploadImg'
					,before: function(obj){
                        //预读本地文件示例，不支持ie8
                        obj.preview(function(index, file, result){
                            //如果需要多图上传 删掉清空操作
                            $("#demo2").empty();
                            $('#demo2').append('<img src="'+ result +'" name="picUrl" id="picUrl" width="100px;" height="100px;" alt="'+ file.name +'" class="layui-upload-img">')
                        });
                    }
					,done: function(res){
					    console.log(res)
						$('#src').val(res.data.src);
						layer.msg('文件上传成功');
					}
				});



		});

        $("#actAdd").bind("submit",function(){
            let picTypeValue = $('#picTypeValue').val();
            if(picTypeValue === "" || picTypeValue === undefined || picTypeValue === null){
                layer.msg("请选择图片类型！");
                return false;
            }
            let data = {
                name:$('#name').val(),
                picSort:$('#picSort').val(),
                picTypeValue:$('#picTypeValue').val(),
                turnUrl:$('#turnUrl').val(),
                picUrl:$('#src').val(),
                picType: $("#picTypeValue").find("option:selected").text()
            };
            $.ajax({
                url: '/addPicManagement',
                data: data,
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    if (data.data == true) {
                        layer.msg('添加成功');
                    } else {
                        layer.msg('添加失败，请刷新页面');
                    }
                    let index = parent.layer.getFrameIndex(window.name);
                    setTimeout(function () {
                        //先得到当前iframe层的索引
                        parent.layer.close(index);
                        parent.layui.table.reload('test', {page: {curr: 1}});
                    }, 3000)

                }
            });
            return false;
        });


	</script>
	</body>
</html>